﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BibleService.Lib
{
	/// <summary>
	/// 节管理
	/// </summary>
	public class Paragraph
	{
		public int ParagraphID{get;set;}
		public int ParagraphNumber { get; set; }
		public int BookID { get; set; }
		public string BookName{get;set;}
		public int ChapterID { get; set; }
		public int ChapterNumber { get; set; }
		public string Content{get;set;}
		public string EnContent{get;set;}
		public string Remark { get; set; }
		
		/// <summary>
		/// 添加记录
		/// </summary>
		public void Add()
		{
			string insertParagraphSql = string.Format("Insert Into Paragraph(ParagraphNumber,BookID,BookName,ChapterID,ChapterNumber,Content,EnContent,Remark) values('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}')",
								this.ParagraphNumber,
								this.BookID,
								this.BookName,
								this.ChapterID,
								this.ChapterNumber,
								this.Content.Replace("'", "''"),
								this.EnContent.Replace("'", "''"),
								this.Remark);
			Lib._SqlHelper.Execute(insertParagraphSql);
		}
		private static List<Paragraph> _getList(System.Data.Common.DbDataReader dataReader)
		{
			List<Paragraph> list = new List<Paragraph>();
			while (dataReader.Read())
			{
				var model = new Paragraph();
				model.ParagraphID = dataReader.GetInt32(0);
				model.ParagraphNumber = dataReader.GetInt32(0);
				model.BookID = dataReader.GetInt32(0);
				model.BookName = dataReader.GetString(0);
				model.ChapterID = dataReader.GetInt32(0);
				model.ChapterNumber = dataReader.GetInt32(0);
				model.Content = dataReader.GetString(0);
				model.EnContent = dataReader.GetString(0);
				model.Remark = dataReader.GetString(0);
			}
			dataReader.Close();
			return list;
		}
		//获取书本指定章的节列表
		public static List<Paragraph> GetParagraphList(Int32 BookID)
		{
			return GetParagraphList(BookID,1);
		}
		public static List<Paragraph> GetParagraphList(Int32 BookID,Int32 ChapterNumber)
		{
			string selectSql = string.Format("select ParagraphID,ParagraphNumber,BookID,BookName,ChapterID ,ChapterNumber ,Content,EnContent,Remark from Paragraph where BookID='{0}' and ChapterNumber='{1}'", BookID, ChapterNumber);
			var dataReader=_SqlHelper.GetList(selectSql);
			return _getList(dataReader);
		}
		
		public static List<Paragraph> GetParagraphListBySearch(String keywords,string Range)
		{
			string selectSql = "select ParagraphID,ParagraphNumber,BookID,BookName,ChapterID ,ChapterNumber ,Content,EnContent,Remark from Paragraph where 1=1 ";
			if(string.IsNullOrEmpty(keywords)==false)
				selectSql+=string.Format(" (Content like '%{0}%' or EnContent like '%{0}%') ",keywords);
			//if(string.IsNullOrEmpty(Range)==false)
			//{				
			//            if (Range.Equals("新约"))
			//            {
			//                        indexList = IndexManager.GetNewTestamentList();
			//            }
			//            if (Range.equals("旧约"))
			//            {
			//                        indexList = IndexManager.GetOldTestamentList();
			//            }
			//            if (Range.equals("摩西五经(创世经-申命记)"))
			//            {
			//                        indexList = IndexManager.GetListByTag("摩西五经");
			//            }
			//            if (Range.equals("历史书(约书亚记-以斯贴记)"))
			//            {
			//                        indexList = IndexManager.GetListByTag("历史书");
			//            }
			//            if (Range.equals("诗歌智慧书(约伯记-雅歌)"))
			//            {
			//                        indexList = IndexManager.GetListByTag("诗歌智慧书");
			//            }
			//            if (Range.equals("大先知书(以赛亚书-但以理书)"))
			//            {
			//                        indexList = IndexManager.GetListByTag("大先知书");
			//            }
			//            if (Range.equals("小先知书(何西亚书-玛拉基书)"))
			//            {
			//                        indexList = IndexManager.GetListByTag("小先知书");
			//            }
			//            if (Range.equals("四福音(马太福音-约翰福音)"))
			//            {
			//                        indexList = IndexManager.GetListByTag("四福音");
			//            }
			//            if (Range.equals("教会历史(使徙行传)"))
			//            {
			//                        indexList = IndexManager.GetListByTag("教会历史");
			//            }
			//            if (Range.equals("其它书信(希伯来书-犹太书)"))
			//            {
			//                        indexList = IndexManager.GetListByTag("其它书信");
			//            }
			//            if (Range.equals("启示录"))
			//            {
			//                        indexList = IndexManager.GetListByTag("启示录");
			//            }
			//}
			var dataReader = _SqlHelper.GetList(selectSql);
			return _getList(dataReader);
		}
		
	}
}
